package gui;

import gui.symbols.SymbolListCellRenderer;
import gui.symbols.SymbolListModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import qbfiles.QbFileHandler;
import qbfiles.QbParser;
import res.Settings;
import res.StringVars;

public class ImportSymbol extends JDialog implements ActionListener{

  private static final long serialVersionUID = 1L;
  private JPanel jContentPane = null;
  private JPanel jButtonPanel = null;
  private JButton jImportButton = null;
  private JButton jCancelButton = null;
  private JPanel jUpperPanel = null;
  private JPanel jFilePanel = null;
  private JLabel jFileLabel = null;
  private JButton jFileButton = null;
  private JScrollPane jScrollPane = null;
  private JList jSymbolList = null;
  private SymbolListModel symbolListModel = null;
  private MainWindow window = null;
  
  private final static String ACTION_CHOOSE_FILE = "choose_file";
  private final static String ACTION_IMPORT = "import";
  private final static String ACTION_CANCEL = "cancel";

  /**
   * @param owner
   */
  public ImportSymbol(MainWindow owner) {
    super(owner);
    window = owner;
    initialize();
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize() {
    this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    this.setSize(453, 251);
    this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/import.png")));
    
    setLocationRelativeTo(null);
    
    this.setModal(true);
    this.setTitle(StringVars.IMPORT_SYMBOL_DIALOG_TITLE);
    this.setContentPane(getJContentPane());
    setVisible(true);
  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel(new BorderLayout());
      jContentPane.add(getJButtonPanel(), BorderLayout.SOUTH);
      jContentPane.add(getJUpperPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes jButtonPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJButtonPanel() {
    if (jButtonPanel == null) {
      jButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
      jButtonPanel.setBorder(new MatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));
      jButtonPanel.add(getJImportButton(), null);
      jButtonPanel.add(getJCancelButton(), null);
    }
    return jButtonPanel;
  }

  /**
   * This method initializes jImportButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getJImportButton() {
    if (jImportButton == null) {
      jImportButton = new JButton(StringVars.IMPORT_SYMBOL_DIALOG_IMPORT_BUTTON);
      jImportButton.addActionListener(this);
      jImportButton.setActionCommand(ACTION_IMPORT);
    }
    return jImportButton;
  }

  /**
   * This method initializes jCancelButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getJCancelButton() {
    if (jCancelButton == null) {
      jCancelButton = new JButton(StringVars.IMPORT_SYMBOL_DIALOG_CANCEL_BUTTON);
      jCancelButton.addActionListener(this);
      jCancelButton.setActionCommand(ACTION_CANCEL);
    }
    return jCancelButton;
  }

  /**
   * This method initializes jUpperPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJUpperPanel() {
    if (jUpperPanel == null) {
      jUpperPanel = new JPanel(new BorderLayout(5, 5));
      jUpperPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
      jUpperPanel.add(getJFilePanel(), BorderLayout.NORTH);
      jUpperPanel.add(getJScrollPane(), BorderLayout.CENTER);
    }
    return jUpperPanel;
  }

  /**
   * This method initializes jFilePanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getJFilePanel() {
    if (jFilePanel == null) {
      jFilePanel = new JPanel(new BorderLayout(5, 5));
      jFilePanel.add(getFileLabel(), BorderLayout.CENTER);
      jFilePanel.add(getJFileButton(), BorderLayout.EAST);
    }
    return jFilePanel;
  }
  
  private JLabel getFileLabel(){
    if(jFileLabel == null){
      jFileLabel = new JLabel();
      jFileLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
    }
    return jFileLabel;
  }

  /**
   * This method initializes jFileButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getJFileButton() {
    if (jFileButton == null) {
      jFileButton = new JButton(StringVars.IMPORT_SYMBOL_DIALOG_CHOOSE_FILE_BUTTON);
      jFileButton.addActionListener(this);
      jFileButton.setActionCommand(ACTION_CHOOSE_FILE);
    }
    return jFileButton;
  }

  /**
   * This method initializes jScrollPane	
   * 	
   * @return javax.swing.JScrollPane	
   */
  private JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
      jScrollPane = new JScrollPane();
      jScrollPane.setViewportView(getJSymbolList());
    }
    return jScrollPane;
  }

  /**
   * This method initializes jSymbolist	
   * 	
   * @return javax.swing.JList	
   */
  private JList getJSymbolList() {
    if (jSymbolList == null) {
      jSymbolList = new JList(getSymbolListModel());
      jSymbolList.setCellRenderer(new SymbolListCellRenderer());
      jSymbolList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    }
    return jSymbolList;
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals(ACTION_CANCEL)){
      dispose();
    }
    else if(e.getActionCommand().equals(ACTION_CHOOSE_FILE)){
      JFileChooser chooser = new JFileChooser();
      File lastPath = Settings.getInstance().getFolder();
      if (lastPath != null)
        chooser.setSelectedFile(lastPath);
      int returnVal = chooser.showOpenDialog(this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        openFile(file);
      }
    }
    else if(e.getActionCommand().equals(ACTION_IMPORT)){
      window.importSymbols(getJSymbolList().getSelectedValues());
      dispose();
    }
  }
  
  private void openFile(File file){
    getFileLabel().setText(file.toString());
    QbFileHandler fileHandler = new QbFileHandler(file);
    QbParser parser = new QbParser(fileHandler.getByteStream());
    parser.getTablePosition();
    
    getJSymbolList().removeAll();
    
    if(fileHandler.createTable()){
      getSymbolListModel().setFields(fileHandler.getFields());
    }
  }

  private SymbolListModel getSymbolListModel(){
    if(symbolListModel == null){
      symbolListModel = new SymbolListModel();
    }
    return symbolListModel;
  }
}
